package com.unity.notificationbackground;
import android.app.Notification;
import android.os.AsyncTask;
import java.util.Iterator;
import java.util.Map;

public class BackgroundNotificationTask extends AsyncTask<Integer, String, String> {
     private static final int FreshTime = 1000;
    public BackgroundNotificationTask() {
        super();
    }

    @Override
    protected String doInBackground(Integer... integers) {
        try {
            while (true) {
                Iterator<Map.Entry<Integer, IBackgroundNotification>> it = AndroidBackgroundNotificationManager.GetInstance().Notificaitons.entrySet().iterator();
                while (it.hasNext())
                {
                    Map.Entry<Integer, IBackgroundNotification> item = it.next();
                    IBackgroundNotification data = item.getValue();
                    if (data.GetNeedClose())
                    {
                        AndroidBackgroundNotificationManager.GetInstance().CancelDisplayedNotification(data.GetId());
                        it.remove();
                    }else if (data.GetNeedUpdate()){
                        Notification.Builder builder = AndroidBackgroundNotificationManager.GetInstance().NewBuilder(data.GetChannelId());
                        AndroidBackgroundNotificationManager.GetInstance().SettingNotification(builder,data);
                        AndroidBackgroundNotificationManager.GetInstance().SendBackgroundNotification(data.GetId(),builder.build());
                        data.OnUpdateNotification();
                    }
                }
                Thread.sleep(FreshTime);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "";
    }

    @Override
    protected void onProgressUpdate(String... values) {
        super.onProgressUpdate(values);
    }
}
